(function() {

  angular.module('cf-content').directive('sidebar', ['$timeout', "$window", function($timeout, $window,EVENT) {
    return {
      restrict: 'AE',
      templateUrl:'app/sidebar/sidebar.html',
      replace:true,
      controller:sidebarController,
      controllerAs:'sidebar'
    }
  }])

  sidebarController.$inject=['$scope','$state','SIDEBAR_ROUTER','assetUrl']

  function sidebarController($scope,$state,SIDEBAR_ROUTER,assetUrl){
    var vm=this;
    vm.logo=assetUrl+'assets/images/logo-pic.png';
    vm.menuList=SIDEBAR_ROUTER;
    init();

    function init(){
      angular.forEach(vm.menuList, function(value) {
            value.active=false;
      });

      angular.forEach(vm.menuList, function(value) {
          angular.forEach(value.submenu,function(submenu){
            if(submenu.sref==$state.current.name){
              value.active=true;
            }
          })
      });

      angular.forEach(vm.menuList, function(value) {
            if (value.active==true) vm.subMenuList=value.submenu;
      });
    }


    $scope.$on('$stateChangeSuccess',function(){
      init();
    })
    vm.openSubmenu=function(menu){
      angular.forEach(vm.menuList, function(value) {
          value.active = false;
      });
      menu.active=true;
      vm.subMenuList=menu.submenu;
    }
  }
  
})();

